
//document.addEventListener("mousedown", function (event) {
//    var el = event.target;
	
    //alert("Clicked element: " + el.id);
//	chrome.extension.sendRequest(el.id);
//}, true); 


//chrome.extension.onRequest.addListener(function(type) {
//	var items = document.getElementById(type);
//	chrome.extension.sendRequest(items);
//});

var backgroundColor;
var prevItem;
var STYLES;
var FOCUS;

$(document).ready(function(){   

	STYLES = {
	    NONE: 'NONE',
	    RED: '-RED', 
	    BLUE: '-BLUE', 
	    DARK: '-DARK', 
	    LIGHT: '-LIGHT', 
	    GREEN: '-GREEN', 
	    JTOOLS: '-JTOOLS', 
	    PLAIN: '-PLAIN', 
	    YOUTUBE: '-YOUTUBE', 
	    CLUETIP: '-CLUETIP', 
	    TIPSY: '-TIPSY', 
	    TIPPED: '-TIPPED', 
	    BOOTSTRAP: '-BOOTSTRAP',
	    ROUNDED: '-ROUNDED'
	};
	 
	FOCUS = {
	    TOP_LEFT: 'TOP_LEFT', 
	    TOP_RIGHT: 'TOP_RIGHT', 
	    TOP_CENTER: 'TOP_CENTER',
	    BOTTOM_LEFT: 'BOTTOM_LEFT', 
	    BOTTOM_RIGHT: 'BOTTOM_RIGHT', 
	    BOTTOM_CENTER: 'BOTTOM_CENTER',
	    RIGHT_CENTER: 'RIGHT_CENTER', 
	    RIGHT_TOP: 'RIGHT_TOP', 
	    RIGHT_BOTTOM: 'RIGHT_BOTTOM',
	    LEFT_CENTER: 'LEFT_CENTER', 
	    LEFT_TOP: 'LEFT_TOP', 
	    LEFT_BOTTOM: 'LEFT_BOTTOM', 
	    CENTER: 'CENTER'
	};
	
//	var style = document.createElement('link');
//	style.rel = 'stylesheet';
//	style.type = 'text/css';
//	style.href = chrome.extension.getURL('jquery.qtip.css');
//	document.head.appendChild(style);
	
	// this breaks finding the elements... 
	//$('head').append('<link rel="stylesheet" href="'+ chrome.extension.getURL('jquery.qtip.css'); +'" type="text/css" />');
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
	
	if(prevItem != null && backgroundColor != null)
	{
		prevItem.style.backgroundColor = backgroundColor;
	}
	
	if (request.count == 1) {
		var name = request.method;
		var returning = "";
		
		var items = document.getElementsByTagName(name);
		if (undefined != items) {
			for (var j = 0; j < items.length; j++) {
				if (items[j].id !== "") {
					if (returning !== "") {
						returning += ",";
					}
					
					returning += items[j].id;
				}
			}
			if (returning == "") {
				returning = "None";
			}
		}
		sendResponse({
			data: returning
		});
	}
	if(request.count == 2){
		
		prevItem = document.getElementById(request.method);
		backgroundColor = prevItem.style.backgroundColor;
		 
		prevItem.style.backgroundColor = "#FDFF47";
	}
	if(request.count == 3)
	{
		var temp = request.method.split("%");
		
		var target = temp[0];
		var title = temp[1];
		var description = temp[2];
		var location = temp[3];
		var tipLocation = temp[4];
		var styleLocation = temp[5];
		var targ = document.getElementById(target);
		addTip(targ, description, title, true, styleLocation, location, tipLocation);
	}
});

function addTip(el, description, title, closeable, style, tipFocus, elFocus) {
	try {
		$(el)	// Deletes the qTip data from the target element allowing for multiple tooltips.
		.removeData('qtip').qtip({
			content: {
				text: description,
				title: {
					text: title,
					button: closeable
				}
			},
			position: {
				at: tipFocus,
				my: elFocus
			},
			show: {
				event: false, // Don't specify a show event...
				ready: true // ... but show the tooltip when ready
			},
			hide: false, // Don't specify a hide event either!
			style: {
				classes: 'ui-tooltip-shadow ui-tooltip' + style
			}
		});
	}catch(exception){
		alert('[addTip] Exception: ' + exception);
	}
}

